package com.znz.web;

import com.znz.domain.*;
import com.znz.service.HospitalService;
import com.znz.service.OrderService;
import com.znz.util.common.Page;
import com.znz.util.common.ResponseJsonUtil;
import com.znz.util.common.ReturnObj;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/hospital")
@Controller
public class HospitalController extends AbstractController {

	@Autowired
	private HospitalService hospitalService;

	@Autowired
	private OrderService orderService;

	private Logger logger = LoggerFactory.getLogger(HospitalController.class);

	/**
	 * 菜单整形项目界面
	 *
	 * @return
	 */
	@RequestMapping(value = {"/", "/hospital"})
	public ModelAndView hospitalList(String hospitaName,Page page) {
		try {
			ModelAndView modelAndView = null;
			Map<String, Object> map = new HashMap<String, Object>();
			Page<Thospital> hospitalArray =hospitalService.getResourcePage(page);
			map.put("hospitalArray", hospitalArray);
			modelAndView = getTemplate("hospital/list", map);
			return modelAndView;
		} catch (Exception e) {
			logger.error("新增医院页面初始化异常", e);
			ModelAndView modelAndView = getTemplate("uncaughtException", null);
			return modelAndView;
		}
	}




	/**
	 * 本方法是新增一个医院账户
	 *
	 * @return
	 */
	@RequestMapping(value = "toAdd")
	private ModelAndView toAdd() {
		try {
			//初始化下拉菜单数据
			Map<String, Object> map = new HashMap<String, Object>();
			//TODO 获取所有部位
			List<Part> partArray = orderService.getPartAll();
			map.put("partArray", partArray);
            if(null!=partArray && partArray.size()>0){
                //TODO 根据部位ID获取项目列表
                List<Project> projectArray = hospitalService.getProjectListByPart(partArray.get(0).getId());
                map.put("projectArray", projectArray);
            }
            ModelAndView modelAndView = getTemplate("hospital/add", map);
			return modelAndView;
		} catch (Exception e) {
			logger.error("新增医院页面初始化异常", e);
			ModelAndView modelAndView = getTemplate("uncaughtException", null);
			return modelAndView;
		}

	}

	@RequestMapping(value = "toEdit")
	private ModelAndView toEdit(Long hospitalId) {
		try {
			//TODO 这里需要根据医院id获取医院对象放到map里
			Map<String, Object> map = new HashMap<String, Object>();
            //TODO 获取所有部位
            List<Part> partArray = orderService.getPartAll();
            map.put("partArray", partArray);
            if(null!=partArray && partArray.size()>0){
                //TODO 根据部位ID获取项目列表
                List<Project> projectArray = hospitalService.getProjectListByPart(partArray.get(0).getId());
                map.put("projectArray", projectArray);
            }
            Thospital thospital = hospitalService.getThospitalById(hospitalId);
            map.put("hospital",thospital);
            String contactStr = thospital.getContacts();
            contactStr = contactStr.replace("[", "").replace("]", "");
            JSONObject jsonObject =JSONObject.fromObject(contactStr);
            HospitalContacts hospitalContacts = (HospitalContacts) JSONObject.toBean(jsonObject,HospitalContacts.class);
            map.put("hospitalContacts",hospitalContacts);
            List<ThospitalProjectAgreement> thospitalProjectAgreementList = hospitalService.getThospitalProjectAgreementListByHospitalId(hospitalId);
            map.put("hospitalProject",thospitalProjectAgreementList);
			//TODO 同时还需要获取项目列表放到map里，用于配置项目列表
			ModelAndView modelAndView = getTemplate("hospital/edit", map);

			return modelAndView;
		} catch (Exception e) {
			logger.error("修改医院页面初始化异常", e);
			ModelAndView modelAndView = getTemplate("uncaughtException", null);
			return modelAndView;
		}
	}

	@RequestMapping(value = "toCheck")
	private ModelAndView toCheck(Long hospital_id) {
		try {
			//TODO 这里需要根据医院id获取医院对象放到map里
			Map<String, Object> map = new HashMap<String, Object>();
			//TODO 获取所有部位
			List<Part> partArray = orderService.getPartAll();
			map.put("partArray", partArray);
			//TODO 根据部位ID获取项目列表
			List<Project> projectArray = hospitalService.getProjectListByPart(1L);
			map.put("projectArray", projectArray);
			//TODO 同时还需要获取项目列表放到map里，用于配置项目列表
			ModelAndView modelAndView = getTemplate("hospital/check", map);
			return modelAndView;
		} catch (Exception e) {
			logger.error("修改医院页面初始化异常", e);
			ModelAndView modelAndView = getTemplate("uncaughtException", null);
			return modelAndView;
		}
	}

	@RequestMapping(value = "getPorjectListByPart")
	@ResponseBody
	public ReturnObj getPorjectListByPart(@RequestParam Long partId) {
		logger.info("/hospital/getPorjectListByPart?partId=" + partId);
		ReturnObj returnObj = null;
		//TODO 根据部位ID获取项目列表
        try{
            if(null!=partId){
                List<Project> projectArray = hospitalService.getProjectListByPart(partId);
                returnObj=ReturnObj.createSuccess(projectArray);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
		return returnObj;
	};


	/**
	 * 添加医院的基础信息
	 * @param hospital
	 * @return
	 */
	@RequestMapping(value = "addHospital")
	@ResponseBody
	public ReturnObj addHospital(@RequestBody String hospital) {
		logger.info("/hospital/addHospital?hospital=" + hospital);
		ReturnObj returnObj = null;
		try {
			JSONObject jsonObject = hospitalService.addHospital(hospital);
			returnObj = ReturnObj.createSuccess(jsonObject);
		} catch (Exception e) {
			logger.error(hospital);
			return ReturnObj.createError();
		}
		return returnObj;
	}

	@RequestMapping(value = "unApproveHospitalList")
	public ModelAndView unApproveHospitalList(@RequestParam String hospitalName, @RequestParam int state,@RequestParam Page page) {
        try {
            //TODO  查看所有已经注册但未审核的医院信息列表
            ModelAndView modelAndView = null;
            Map<String, Object> map = new HashMap<String, Object>();
            Page<Thospital> thospitalPage = hospitalService.getUnApproveHospitalListByName(hospitalName, state,page);
            map.put("thospitalPage", thospitalPage);
            modelAndView = getTemplate("hospital/check", map);
            return modelAndView;
        } catch (Exception e) {
            logger.error("页面初始化异常", e);
            ModelAndView modelAndView = getTemplate("uncaughtException", null);
            return modelAndView;
        }
	}




	/**
	 * 医院分页列表
	 * @return
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public ReturnObj list(@Valid Page page) {
		ReturnObj returnObj = new ReturnObj();
		try {
			return ReturnObj.createSuccess(hospitalService.getResourcePage( page));
		} catch (Exception e) {
			e.getCause();
			ResponseJsonUtil.paramContentError(returnObj, "系统繁忙");
		}
		return returnObj;
	}
    @RequestMapping(value = "/removeHospital")
    @ResponseBody
    public ReturnObj  removeHospitalById(Long hospitalId){
         ReturnObj returnObj=new ReturnObj();
        try{
            if(null!=hospitalId){
                boolean flag=hospitalService.removeHospitalById(hospitalId);
                return  ReturnObj.createSuccess(flag);
            }
        }catch (Exception e){
            e.getCause();
            ResponseJsonUtil.paramContentError(returnObj,"删除失败！请联系管理员");
        }
        return returnObj;
    }
    @RequestMapping(value = "/editHospital")
    public ReturnObj editeHospital(String hospital){
        ReturnObj returnObj=new ReturnObj();
        try{
            Thospital thospital=new Thospital();

        }catch (Exception e){

        }
        return  returnObj;
    }

    /**
     * 保存医院认证信息
     * @param hospitalId    医院ID
     * @return
     */
    @RequestMapping(value = "")
    public  ReturnObj approveHospital(@RequestBody Long hospitalId){
        ReturnObj returnObj = null;
        boolean flag=false;
        try {
            flag = hospitalService.approveHospital(hospitalId);
            returnObj = ReturnObj.createSuccess(flag);
        } catch (Exception e) {
            returnObj=ReturnObj.createError();
        }
        return returnObj;
    }
}
